package com.vv.vv_commonlibrary.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vv.vv_commonlibrary.R;
import com.vv.vv_commonlibrary.page.HandlerPageListener;


/**
 * 用于处理下拉刷新，加载更多，数据为空的业务逻辑
 *
 * @author xia
 */
public class EmptyWithRefreshLayout extends FrameLayout implements OnRefreshListener, OnLoadMoreListener, HandlerPageListener {
    @LayoutRes
    private int emptyLayoutId;
    @StringRes
    private int emptyTips;
    private Context context;

    /**
     * 判断是否是刷新状态加载数据
     */
    private boolean isRefresh;
    private View errorView;
    private TextView emptyView;
    private SmartRefreshLayout refreshLayout;
    private RecyclerView recycleView;
    private LoadDataListener loadDataListener;

    public void setLoadDataListener(LoadDataListener loadDataListener) {
        this.loadDataListener = loadDataListener;
    }

    public EmptyWithRefreshLayout(@NonNull Context context) {
        this(context, null);
    }

    public EmptyWithRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(@NonNull Context context, AttributeSet attrs) {
        this.context = context;
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.EmptyWithRefreshLayout);
        emptyLayoutId = attributes.getResourceId(R.styleable.EmptyWithRefreshLayout_emptyLayoutId, R.layout.empty_default_layout);
        emptyTips = attributes.getResourceId(R.styleable.EmptyWithRefreshLayout_emptyTips, R.string.empty_default_tips);
        boolean isAutoRefresh = attributes.getBoolean(R.styleable.EmptyWithRefreshLayout_isAutoRefresh, false);
        attributes.recycle();

        LayoutInflater.from(context).inflate(R.layout.refresh_with_loadmore_layout, this);

        refreshLayout = findViewById(R.id.baseRefreshLayout);
        recycleView = findViewById(R.id.baseRecycleView);

        errorView = findViewById(R.id.errorView);

        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);

        if (context instanceof LoadDataListener) {
            loadDataListener = (LoadDataListener) context;
        }

        if (isAutoRefresh) {
            refreshLayout.autoRefresh();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (!isRefresh) {
            isRefresh = true;
            if (loadDataListener != null) {
                loadDataListener.loadDataFromServer();
            }
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if (loadDataListener != null && !isRefresh) {
            loadDataListener.loadDataFromServer();
        }
    }

    /**
     * 刷新数据
     */
    public void autoRefresh() {
        refreshLayout.setEnableRefresh(true);
        refreshLayout.autoRefresh();
    }

    public void setEnableRefresh(boolean isRefresh) {
        refreshLayout.setEnableRefresh(isRefresh);
    }

    public void setRefresh(boolean isRefresh) {
        this.isRefresh = isRefresh;
    }

    @Override
    public boolean isRefresh() {
        return isRefresh;
    }


    /**
     * 请求失败的操作，没有网络等情况
     */
    public void refreshError() {
//        finishRefresh();
        refreshLayout.finishLoadMore();
        if (isRefresh && recycleView.getAdapter().getItemCount() == 0) {
            errorView.setVisibility(VISIBLE);
        }
        refreshLayout.finishRefresh();
        isRefresh = false;
    }

    /**
     * 完成刷新的操作
     */
    @Override
    public void finishRefresh() {
        errorView.setVisibility(GONE);
        refreshLayout.finishRefresh();
        isRefresh = false;
    }


    /**
     * 请求数据为空时，显示的界面
     */
    @Override
    public void showEmptyView() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        if (emptyView == null) {
            LayoutInflater.from(context).inflate(emptyLayoutId, this);
            emptyView = findViewById(R.id.emptyView);
            emptyView.setText(emptyTips);
        } else {
            emptyView.setVisibility(VISIBLE);
        }

    }

    public void hideEmptyView() {
        if (emptyView != null) {
            emptyView.setVisibility(GONE);
        }
    }

    @Override
    public void refreshHaveMoreData() {
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.finishLoadMore();
    }


    @Override
    public void handlerNoMoreData() {
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.finishLoadMoreWithNoMoreData();
    }


    public interface LoadDataListener {

        /**
         * 处理下拉刷新及加载更多是，去服务器请求数据
         */
        void loadDataFromServer();
    }

}
